package com.siyao.redvip.components.base;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.siyao.redvip.R;
import com.siyao.redvip.RedApplication;
import com.siyao.redvip.components.view.CustomProgressDialog;
import com.siyao.redvip.components.view.LoadingDialog;
import com.siyao.redvip.ui.activity.PhotoActivity;
import com.siyao.redvip.ui.activity.RedvipMainActivity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public abstract class BaseActivity extends Activity implements OnClickListener {
	
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	protected LayoutParams layoutParams;
	protected RelativeLayout appTopView, appMainView;
	public DisplayImageOptions options;
	protected Gson gson = new Gson();
	protected LoadingDialog pd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(getWindow().FEATURE_NO_TITLE);
		setContentView(R.layout.app_base_layout);
		appTopView = (RelativeLayout) findViewById(R.id.app_layout_top);
		appMainView = (RelativeLayout) findViewById(R.id.app_layout_main);
		layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
	    options = new DisplayImageOptions.Builder()
	    .showImageOnLoading(R.drawable.ic_loading)
	    .showImageForEmptyUri(R.drawable.ic_load_faild)
	    .showImageOnFail(R.drawable.ic_load_faild)
	    .cacheInMemory(true)
	    .cacheOnDisc(true)
	    .considerExifParams(true)
	    .bitmapConfig(Bitmap.Config.RGB_565)
	    .build();
	    
		pd = new LoadingDialog(this);
		pd.setCancelable(false);
		beforeInit();
		setContentView();
		initComponent();
		initData();
		
	}

	@Override
	protected void onStop() {
		super.onStop();
		dismissPd();
	}

	protected void showPd() {
		try {
			if (pd != null && !pd.isShowing()) {
				pd.show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void dismissPd() {
		try {
			if (pd != null && pd.isShowing()) {
				pd.dismiss();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	/**
	 * 设置顶部的文字内容
	 * @param titleText
	 */
	protected void setTitleText(String titleText) {
		if (null == appTopView) {
			return;
		}
		if (null == titleText || "".equals(titleText)) {
			return;
		}
		
		TextView textView =(TextView) findViewById(R.id.top_center_tv);
		textView.setText(titleText);
		if(appTopView.getVisibility()!= View.VISIBLE){
			appTopView.setVisibility(View.VISIBLE);
		}
		textView.setVisibility(View.VISIBLE);
	}
	/**
	 * 设置顶部的文字内容
	 * @param titleText
	 */
	protected void setTitleRightButton(String buttonText) {
        if (null == appTopView) {
            return;
        }
        if (null == buttonText || "".equals(buttonText)) {
            return;
        }

        Button rightButton = (Button) findViewById(R.id.top_right_btn);
        rightButton.setOnClickListener(this);
        rightButton.setText(buttonText);

        if (appTopView.getVisibility() != View.VISIBLE) {
            appTopView.setVisibility(View.VISIBLE);
        }

        rightButton.setVisibility(View.VISIBLE);

    }
	
	protected void setTitleLeftButton(String buttonText) {
        if (null == appTopView) {
            return;
        }

        if (null == buttonText || "".equals(buttonText)) {
            return;
        }

        Button leftButton = (Button) findViewById(R.id.top_left_btn);
        leftButton.setOnClickListener(this);

        leftButton.setText(buttonText);

        if (appTopView.getVisibility() != View.VISIBLE) {
            appTopView.setVisibility(View.VISIBLE);
        }

        leftButton.setVisibility(View.VISIBLE);

    }

	/**
	 * 加载layout之前要做的事情
	 */
	 protected abstract void beforeInit();
		
	/**
	 * 加载layout
	 */
	protected abstract void setContentView();
	 
	/**
	 * 初始化view控件
	 */
	protected abstract void initComponent();
	
	/**
	 * 加载数据
	 */
	protected abstract void initData();

	
	@Override
	public void onClick(View v) {

		int viewId=v.getId();
		Intent intent;
		switch (viewId) {
		case R.id.top_left_btn:
			 intent = new Intent(this, RedvipMainActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}

}
